package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.UserEntity;
import dongbai.soft.service.UserService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.R;
import dongbai.soft.utils.getUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;
import java.util.Objects;


/**
 * @author SingDeer
 * @email 3335678582@qq.com
 * @date 2023-06-20 14:31:40
 */
@RestController
@RequestMapping("")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * 重复登录生成重复Cookie，不予解决，但是限制有效时间
     */

    @RequestMapping("/login")
    public R login(@RequestBody Map<String, String> maps) {
        UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("username", maps.get("username")));
        if (userEntity != null && userEntity.getPassword().equals(maps.get("password"))) {
            userEntity.setPassword("");
            return Objects.requireNonNull(R.ok().put("token", userService.SetCookie(userEntity.getUid(), userEntity.getRole().toString()))).put("user", userEntity);
        }
        return R.error(Constant.LOGIN_FAILED);
    }


    /**
     * 新建用户，只有分站管理员可以被调度中心新建
     */
    @RequestMapping("/newuser")
    //@RequiresPermissions(":user:save")
    public R save(@RequestBody UserEntity user) {
        if (userService.getOne(new QueryWrapper<UserEntity>().eq("username", user.getUsername())) != null) {
            return R.error(Constant.USER_DUP);
        }
        user.setUid(getUUID.get6UUID());
        user.setStartDate(new Date());
        user.setPassword(Constant.DEFALULT_PWD);
        user.setStatus(Constant.STATE_Y);
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改密码
     */
    @RequestMapping("/editpass")
    public R changepwd(@RequestBody Map<String, String> maps) {
        UserEntity userEntity = userService.getById(maps.get("UID"));
        if (userEntity.getPassword().equals(maps.get("oldpassword"))) {
            userEntity.setPassword(maps.get("password"));
            userService.updateById(userEntity);
            return R.ok();
        } else
            return R.error(Constant.CPWD_FAILED);
    }

    /**
     * 修改个人信息
     */
    @RequestMapping("/editinfo")
    public R update(@RequestBody Map<String, String> maps) {
        userService.update(new UpdateWrapper<UserEntity>().eq("UID", maps.get("UID")).set("name", maps.get("name")).set("phone", maps.get("phone")));
        return R.ok();
    }


    /**
     * 查看单个用户除密码以外的所有信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map<String, String> maps) {
        UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("UID", maps.get("UID")).select(UserEntity.class, info -> !info.getColumn().equals("password")));
        return R.ok().put("user", user);
    }

    /**
     * 调度中心查看分站的所有管理员信息
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> maps) {
        return R.ok().put("page", userService.queryPage(maps));
    }

    /**
     *
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Map<String, String> maps) {
        userService.update(new UpdateWrapper<UserEntity>().eq("UID", maps.get("UID")).set("status", Constant.STATE_N));
        return R.ok();
    }

}
